local ClientOHOH =_G.ClientOHOH
local M = {}
local shopPanel = nil
local CallBackManager = ClientOHOH.load.CallBackManager()
local LanguageHelper=_G.LanguageHelper 
local listUiData = {}

function Initsts()
    local ui = {
        gPackage={},
        list={},
        gSideTag = "ctrl.sideBar.gamepasses"
    }
    for i=1,2 do
        ui.gPackage[i] = {
            buyBtn = "ctrl.gamepass.Package."..i..".btn",
            price = "ctrl.gamepass.Package."..i..".price"
        }
    end
    local listName = {
        "Luck",
        "MegaLuck",
        "OctupleOpen",
        "Pet4",
        "Pet8",
        "PetPack50",
        "TripleOpen",
        "UltraLuck",
        "VoidCompletion",
        "VoidHalving"
    }
    for _, value in pairs(listName) do
        ui.list[value] = {
            buyBtn = "ctrl.gamepass.list."..value..".btn",
            price = "ctrl.gamepass.list."..value..".price"
        }
    end
    return ui
end

function M.GetInitUiList()
    return Initsts()
end

function M.Init(ShopPanel)
    shopPanel = ShopPanel
    CallBackManager:AddRbxConnect(shopPanel,shopPanel.insts.gamepass.gSideTag.Activated,function()
        shopPanel:ScrollingGo(shopPanel.insts.gamepass.gSideTag.X.Value)
    end)
end

function M.UpdateGamePass(gamePassData)
    for _,v in pairs(listUiData) do
        v:update(gamePassData)
    end
end

function M.isGamePassGamePassPack(data)
    if #data>0 then
        for _,v in pairs(data) do
            if v>0 then
                return true
            end
        end
    end

    return false
end


function M.Full(data)
    local packIndex = 0
    for index, value in pairs(data) do
        -- if #value.des >0 then
        if M.isGamePassGamePassPack(value.des)then

            packIndex = packIndex+1
            M.FullGamePassPack(value,packIndex)
        else
            M.FullGamePassList(value)
        end
    end

end

function M.FullGamePassPack(data,packIndex)

    -- print("-------FullGamePassPack------------",packIndex,data.id,data.gid,data.item_id)
    local gamepassUi = shopPanel.insts.gamepass
    local connect = nil
    if data.isHad then
        gamepassUi.gPackage[packIndex].price.Text = LanguageHelper:GetText(20608)
    else
        gamepassUi.gPackage[packIndex].price.Text = data.price
        connect = CallBackManager:AddRbxConnect(shopPanel,gamepassUi.gPackage[packIndex].buyBtn.Activated,function()
            shopPanel.ShopBuyEvent:Action(data.buyInfo)
        end)
    end

    
    data.DataChange:Connect(function(isHad)
        data.isHad = isHad
        if data.isHad then
            gamepassUi.gPackage[packIndex].price.Text=LanguageHelper:GetText(20608)
            connect:Disconnect()
        end
    end)
end


function M.FullGamePassList(data)
    local gamepassUi = shopPanel.insts.gamepass
    if data.isHad then
        gamepassUi.list[data.item_id].price.Text = LanguageHelper:GetText(20608)
    else
        gamepassUi.list[data.item_id].price.Text =data.price
        CallBackManager:AddRbxConnect(shopPanel,gamepassUi.list[data.item_id].buyBtn.Activated,function()
            shopPanel.ShopBuyEvent:Action(data.buyInfo)
        end)
    end
    table.insert(listUiData,{
        ui=gamepassUi.list[data.item_id],
        cfg=data,
        update=function(this,data)
            if data[this.cfg.name] then
                this.cfg.isHad = true
            end
            if this.cfg.isHad then
                this.ui.price.Text=LanguageHelper:GetText(20608)
            end
        end
    })
end

function M.Get()
    return M
end

return M 